package com.azt.provider.impl;
import com.azt.api.pojo.FriendlyLink;
import com.azt.api.service.FriendlyLinkService;
import com.azt.model.page.Pagination;
import com.azt.provider.mapper.FriendlyLinkMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class FriendlyLinkServiceImpl  implements FriendlyLinkService{

	@Autowired
	private FriendlyLinkMapper mapper;
	
	@Override
	public Pagination getFriendlyLinkPage(int pageno, int pagesize, Map<String, String> searchMap) {
		Page<Object> startPage = PageHelper.startPage(pageno, pagesize);
		List<FriendlyLink> friendlyLinkList = mapper.searchFriendlyLink(searchMap);
		Pagination p = new Pagination(startPage, friendlyLinkList);
		p.setList(friendlyLinkList);
		p.setParams(searchMap);
		return p;
	}

	@Override
	public FriendlyLink getFriendlyLinkById(Integer id) {
		return mapper.selectByPrimaryKey(id);
	}

	@Override
	public void saveOrUpdateFriendlyLink(FriendlyLink friendlyLink) {
		if(friendlyLink.getId() == null) mapper.insert(friendlyLink);
		else mapper.updateByPrimaryKey(friendlyLink);
	}

	@Override
	public void deleteFriendlyLink(FriendlyLink friendlyLink) {
		if(friendlyLink != null) {
			mapper.delete(friendlyLink);
		}
	}

	@Override
	public List<FriendlyLink> findIndexFriendlyLink() {
		FriendlyLink link = new FriendlyLink();
		link.setState(1);
		Example example = new Example(FriendlyLink.class);
		example.createCriteria().andEqualTo(link);
		example.orderBy("type desc, seq asc");
		return mapper.selectByExample(example);
	}
	
}

